package org.example.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.CollectPractice;
import org.example.commonapi.dto.LoginInfo;
import org.example.commonapi.domain.R;
import org.example.commonapi.dto.PageInfo;
import org.example.commonapi.dto.PracticeInfoDto;
import org.example.commonapi.util.JwtUtil;
import org.example.user.service.CollectPracticeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class CollectPracticeController {
    @Resource
    private CollectPracticeService collectPracticeService;

    @Resource
    private JwtUtil jwtUtil;
    /**
     *查询收藏的练习id,可以放到service层(要重写,先用着)
     * @param request
     * @return
     */
    @GetMapping("/list/collectpracticeIds")
    public R collectPracticeIdList(Integer difficulty, Integer type, String name, String subject,HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        List<PracticeInfoDto> practiceInfoDtos=collectPracticeService.collectPracticeIdList(difficulty,type,name,subject,loginInfo.getId());
        //只需要id
        List<Long> practiceIds=new ArrayList<>();
        for(PracticeInfoDto practiceInfoDto:practiceInfoDtos){
            practiceIds.add(practiceInfoDto.getId());
        }
        return R.success(practiceIds);
    }

    /**
     * 单个删除收藏练习
     * @param practiceId
     * @param request
     * @return
     */
    @DeleteMapping("/delete/collectpractice/{practiceId}")
    public R deleteCollectPractice(@PathVariable("practiceId") Long practiceId,HttpServletRequest request) {
        LoginInfo loginInfo = jwtUtil.parseJwt(request.getHeader("token"));
        collectPracticeService.deleteCollectPractice(practiceId, loginInfo.getId());
        return R.success();
    }

    /**
     * 批量删除收藏练习
     * @param practiceIdList
     * @param request
     * @return
     */
    @DeleteMapping("/deletelist/collectpractice")
    public R deleteCollectPracticeList(@RequestBody List<Long> practiceIdList, HttpServletRequest request) {
//        转换为List给mybatis-plus
        if(practiceIdList.isEmpty())return R.error("删除失败");

//        解析出登录信息
        LoginInfo loginInfo = jwtUtil.parseJwt(request.getHeader("token"));
        log.info("删除用户{}题目id为:{}",loginInfo.getId(),practiceIdList);
//        删除条件
        LambdaQueryWrapper<CollectPractice> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(CollectPractice::getPracticeId,practiceIdList).eq(CollectPractice::getUserId,loginInfo.getId());
//        条件删除
        collectPracticeService.remove(queryWrapper);
        return R.success();
    }


    /**
     * 查询收藏的练习
     * @param request
     * @return
     */
    @GetMapping("/list/collectpractice")
    public R collectPracticeList(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,Integer difficulty, Integer type, String name, String subject,HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        PageInfo practiceInfoDtos=collectPracticeService.collectPracticeList(pageNum,pageSize,difficulty,type,name,subject,loginInfo.getId());
        return R.success(practiceInfoDtos);
    }

}
